<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 拿出最少数目的魔法豆 -->
    <script>
      var minimumRemoval = function (beans) {
        let sum = beans.reduce((cur, prev) => cur + prev, 0)
        let len = beans.length
        beans.sort((a, b) => a - b)
        let ans = sum
        for (let i = 0; i < beans.length; i++) {
          ans = Math.min(ans, sum - (len - i) * beans[i])
        }
        return ans
      }
      console.log(minimumRemoval([4, 1, 6, 5]))
    </script>
  </body>
</html>
